package com.bbva.gptf.back.dominio;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity(versionField = "", table = "TGPT001_FTE_FICHERO", schema = "DBSGPT")
@RooDbManaged(automaticallyDelete = true)
public class Tgpt001FteFichero {
	public static Tgpt001FteFichero findTgpt001FteFicheroByNbFuente(
			final String nbFuente) {
		if (nbFuente == null) {
			return null;
		}
		return entityManager()
				.createQuery(
						"SELECT o FROM Tgpt001FteFichero o WHERE nbFuente = :nbFuente",
						Tgpt001FteFichero.class)
				.setParameter("nbFuente", nbFuente).getSingleResult();
	}

	public static List<Tgpt001FteFichero> obtieneFuenteFicheroInsumos() {
		try {

			final StringBuffer qlString = new StringBuffer(
					"FROM Tgpt001FteFichero ff ");
			qlString.append("WHERE ff.cdFuente in ( ");
			qlString.append(" SELECT c.cdCarga.cdFichero.cdFuente.cdFuente ");
			qlString.append(" FROM Tgpt048Carga c");
			qlString.append(")");

			return entityManager().createQuery(qlString.toString(),
					Tgpt001FteFichero.class).getResultList();

		} catch (final Exception e) {
			// TODO: handle exception
		}
		return new ArrayList<Tgpt001FteFichero>();
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "sq_fuente_fichero")
	@SequenceGenerator(name = "sq_fuente_fichero", sequenceName = "SQ_FUENTE_FICHERO")
	@Column(name = "CD_FUENTE")
	private BigDecimal cdFuente;
}
